package com.neu.fenli2023623.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neu.fenli2023623.entity.FeeDetails;
import com.neu.fenli2023623.entity.Mzcz;
import com.neu.fenli2023623.service.MzczService;
import com.neu.fenli2023623.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mzcz")
public class MzczController {

    @Autowired
    private MzczService mzczService;

    //添加备注
    @RequestMapping("AddNote")
    public ResultVo mzczUpdate(@RequestBody Mzcz mzcz){
        int i = mzczService.updateAddNote(mzcz);
        if (i>0){
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.ok("更新失败");
        }

    }
//    查询全部
    @RequestMapping("selectall")
    public ResultVo mzczSelect() {
        List<Map<String, Object>> mzczs = mzczService.selectAll();
        if (mzczs != null) {
            // 创建MzczResult对象

            // 将MzczResult对象转换为JSON格式
            ObjectMapper objectMapper = new ObjectMapper();
            String mzczResultJson = null;
            try {
                mzczResultJson = objectMapper.writeValueAsString(mzczs);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                return ResultVo.error("转换JSON格式失败");
            }

            // 返回JSON格式数据给前端
            return ResultVo.ok("查找成功").data("mzczall",mzczResultJson);
        } else {
            return ResultVo.error("查找失败");
        }
    }
//根据时间范围查询
    @RequestMapping("selectByDateRange")
    public ResultVo mzczselectByDateRange(@RequestParam String startDate, @RequestParam String endDate) {
        // 调用Service层方法，根据日期期间查询数据
        List<Map<String, Object>> mzczs = mzczService.selectByDateRange(startDate, endDate);

        if (mzczs != null) {
            // 将查询结果转换为JSON格式
            ObjectMapper objectMapper = new ObjectMapper();
            String mzczResultJson;
            try {
                mzczResultJson = objectMapper.writeValueAsString(mzczs);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                return ResultVo.error("转换JSON格式失败");
            }

            // 返回JSON格式数据给前端
            return ResultVo.ok("查询成功").data("mzczselectByDateRange",mzczResultJson);
        } else {
            return ResultVo.error("查询失败");
        }
    }
    //这里的患者姓名和就诊卡号的参数就用那个输入框里的值传进来，赋一样的值就好
    @RequestMapping("selectByPatient")
    public ResultVo mzczselectByPatient(@RequestParam("patientName") String patientName, @RequestParam("cardNumber") String cardNumber) {
        // 调用Service层方法，根据患者姓名或就诊卡号查询数据
        List<Map<String, Object>> mzczs = mzczService.selectByPatient(patientName, cardNumber);

        if (mzczs != null) {
            // 将查询结果转换为JSON格式
            ObjectMapper objectMapper = new ObjectMapper();
            String mzczResultJson;
            try {
                mzczResultJson = objectMapper.writeValueAsString(mzczs);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                return ResultVo.error("转换JSON格式失败");
            }

            // 返回JSON格式数据给前端
            return ResultVo.ok("查询成功").data("ByPatient",mzczResultJson);
        } else {
            return ResultVo.error("查询失败");
        }
    }

    //这里的患者姓名和就诊卡号的参数就用那个输入框里的值传进来，赋一样的值就好,日期可为空
    @RequestMapping("selectByDateRangeOrPatient")
    public ResultVo mzczselectByDateRangeOrPatient(@RequestParam String startDate, @RequestParam String endDate,
                                                    @RequestParam("patientName") String patientName,
                                                    @RequestParam("cardNumber") String cardNumber) {
        // 调用Service层方法，根据患者姓名或就诊卡号查询数据
        List<Map<String, Object>> mzczs = mzczService.selectByDateRangeOrPatient(startDate, endDate,patientName, cardNumber);

        if (mzczs != null) {
            // 将查询结果转换为JSON格式
            ObjectMapper objectMapper = new ObjectMapper();
            String mzczResultJson;
            try {
                mzczResultJson = objectMapper.writeValueAsString(mzczs);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                return ResultVo.error("转换JSON格式失败");
            }

            // 返回JSON格式数据给前端
            return ResultVo.ok("查询成功").data("ByDateRangeOrPatient",mzczResultJson);
        } else {
            return ResultVo.error("查询失败");
        }
    }





}
